
define(function(require, exports, module) {
	
	var $ = require("$");
	var utils = require("utils");
	
	$(function(){
		$('body').append('<div id="ueditorContainer" style="display:none;"></div>');
	});
	
	//过滤ueditor的图片,附件等的保存地址,将临时文件位置修改成最终保存目录
	//ueditor:ueditor对象
	//beforeStr:临时目录
	//afterStr:目标目录
	//oldUrlList:用的时候传进一个空的数组，该数组保存旧url
	//urlList:用的时候传进一个空的数组，该数组保存新url
	//返回修饰后的ueditor内容，直接将该内容保存到数据库即可
	var ueditorUrlFilter = function(ueditor, beforeStr, afterStr, oldUrlList, urlList){
		
		$("#ueditorContainer").html(ueditor.getContent());
		var afterUrl = '';
		var fileName = '';
		$("#ueditorContainer").find("img").each(function(i){
			afterUrl = $(this).attr("src")+'';
			if(afterUrl.indexOf(beforeStr) > -1){
				oldUrlList.push(afterUrl);
				//获取url中的文件名
				fileName=afterUrl.replace(/(.*\/)*([^.]+).*/ig,"$2");
				afterUrl = afterUrl.substring(afterUrl.indexOf(beforeStr), afterUrl.length);
				afterUrl = afterUrl.replace(beforeStr, afterStr);
				afterUrl = afterUrl.replace(fileName, new Date().getTime() + utils.random.uuid(12));
				$(this).attr("src", afterUrl);
				$(this).attr("_src", afterUrl);
				urlList.push(afterUrl);
			}
		});
		$("#ueditorContainer").find("a").each(function(i){
			afterUrl = $(this).attr("href")+'';
			if(afterUrl.indexOf(beforeStr) > -1){
				oldUrlList.push(afterUrl);
				//获取url中的文件名
				fileName=afterUrl.replace(/(.*\/)*([^.]+).*/ig,"$2");
				afterUrl = afterUrl.substring(afterUrl.indexOf(beforeStr), afterUrl.length);
				afterUrl = afterUrl.replace(beforeStr, afterStr);
				afterUrl = afterUrl.replace(fileName, new Date().getTime() + utils.random.uuid(12));
				$(this).attr("href", afterUrl);
				$(this).attr("_href", afterUrl);
				urlList.push(afterUrl);
			}
		});
		
		return $("#ueditorContainer").html();
	}
	
	var ueditorPlainTxt = function(ueditor, beforeStr, afterStr, oldUrlList, urlList){
		
		$("#ueditorContainer").html(ueditor.getPlainTxt());
		var afterUrl = '';
		var fileName = '';
		$("#ueditorContainer").find("img").each(function(i){
			afterUrl = $(this).attr("src")+'';
			if(afterUrl.indexOf(beforeStr) > -1){
				oldUrlList.push(afterUrl);
				//获取url中的文件名
				fileName=afterUrl.replace(/(.*\/)*([^.]+).*/ig,"$2");
				afterUrl = afterUrl.substring(afterUrl.indexOf(beforeStr), afterUrl.length);
				afterUrl = afterUrl.replace(beforeStr, afterStr);
				afterUrl = afterUrl.replace(fileName, new Date().getTime() + utils.random.uuid(12));
				$(this).attr("src", afterUrl);
				$(this).attr("_src", afterUrl);
				urlList.push(afterUrl);
			}
		});
		$("#ueditorContainer").find("a").each(function(i){
			afterUrl = $(this).attr("href")+'';
			if(afterUrl.indexOf(beforeStr) > -1){
				oldUrlList.push(afterUrl);
				//获取url中的文件名
				fileName=afterUrl.replace(/(.*\/)*([^.]+).*/ig,"$2");
				afterUrl = afterUrl.substring(afterUrl.indexOf(beforeStr), afterUrl.length);
				afterUrl = afterUrl.replace(beforeStr, afterStr);
				afterUrl = afterUrl.replace(fileName, new Date().getTime() + utils.random.uuid(12));
				$(this).attr("href", afterUrl);
				$(this).attr("_href", afterUrl);
				urlList.push(afterUrl);
			}
		});
		
		return $("#ueditorContainer").html();
	}
	
	var ueditorSetContent = function(ueditor,content){
		ueditor.setContent(content, false);
	}
	
	//过滤Ueditor 修改时添加前后空行
	var contentFilter = function(content) {
		var index = content.indexOf('<p>')
		var firstP = content.substring(index+3,content.indexOf('</p>',index));
		firstP = firstP.trim(); 
		if(firstP == ""){
			content = content.substring(content.indexOf('</p>',index+4),content.length);
		}
		
		var lastIndex = content.lastIndexOf('<p>');
		var lastP = content.substring(lastIndex+3,content.indexOf('</p>',lastIndex));
		lastP = lastP.trim();
		if(lastP == ''){
			content = content.substring(0,lastIndex);
		}
		
		return content ;
	}
	
	
	module.exports = {
			ueditorUrlFilter : ueditorUrlFilter,
			ueditorSetContent:ueditorSetContent,
			ueditorPlainTxt  : ueditorPlainTxt,
			contentFilter    : contentFilter
	};

});